package org.shoukaiseki.supersaiyan.generator.config;

import org.shoukaiseki.common.exception.MessageVirtualException;
import org.shoukaiseki.common.function.IGetter;
import org.shoukaiseki.common.utils.BooleanUtils;
import org.shoukaiseki.common.utils.StringUtils;
import org.shoukaiseki.supersaiyan.common.utils.spring.SpringUtils;
import org.shoukaiseki.supersaiyan.generator.domain.GenResourceConf;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.shoukaiseki.common.utils.BeanUtils.convertToFieldName;
import static org.shoukaiseki.common.utils.StringUtils.objToString;

/**
 * 读取代码生成相关配置
 *
 * @author ruoyi
 */
public class GenConfigProxy
{



    //本次获取配置的资源名,如果为空或者null则会忽略
    private String resourceConfName;

    GenResourceConf resourceConf ;

    BaseGenConfig baseGenConfig=new BaseGenConfig();

    private Map<String,String>  paramMap=new LinkedHashMap<>();

    public GenConfigProxy(String resourceConfName){
        this.resourceConfName=resourceConfName;
        if(StringUtils.isEmpty(resourceConfName)){
            this.resourceConfName= SpringUtils.getBean(BaseGenConfig.class).getResourceConfName();
        }
        this.init();
    }

    public GenConfigProxy(){
        this.resourceConfName=SpringUtils.getBean(BaseGenConfig.class).getResourceConfName();
        this.init();
    }

    public void init(){
        baseGenConfig = getBaseGenConfig();
        if(StringUtils.isEmpty(this.resourceConfName)){
            throw new MessageVirtualException("resourceConfName 不能为空");
        }
        List<GenResourceConf> list = baseGenConfig.getResourceConfList().stream().filter((o) -> StringUtils.equals(o.getName(), this.resourceConfName)).collect(Collectors.toList());
        if (list.size()==0) {
            throw new MessageVirtualException("resourceConfName对应的名称不存在,resourceConfName={}").format(this.resourceConfName);
        }
        if (list.size()>1) {
            throw new MessageVirtualException("resourceConfName对应的名称存在多个,resourceConfName={}").format(this.resourceConfName);
        }
        resourceConf=list.get(0);

        String propName = convertToFieldName(GenConfigProxy::getAuthor);
        addParamMap(propName,baseGenConfig.getAuthor());

        propName = convertToFieldName(GenConfigProxy::getPackageName);
        addParamMap(propName,baseGenConfig.getPackageName());

        propName = convertToFieldName(GenConfigProxy::getAutoRemovePre);
        addParamMap(propName,baseGenConfig.isAutoRemovePre());

        propName = convertToFieldName(GenConfigProxy::getTablePrefix);
        addParamMap(propName,baseGenConfig.getTablePrefix());

        propName = convertToFieldName(GenConfigProxy::isColumnCamelCaseConvert);
        addParamMap(propName,baseGenConfig.isColumnCamelCaseConvert());

        for (GenResourceConf child : resourceConf.getChildren()) {
            addParamMap(child.getName(),child.getValue());
        }
    }

    /**
     * 添加到属性map中,如果不存在则使用父级的值
     * @param propName
     * @param parentValue
     */
    public void addParamMap(String propName,Object parentValue){
        GenResourceConf children = getChildrenByPropName(propName);
        if (children ==null) {
            paramMap.put(propName,objToString(parentValue));
        }else{
            paramMap.put(propName,children.getValue());
        }
    }

    public GenResourceConf getChildrenByPropName(String propName){
        List<GenResourceConf> list = resourceConf.getChildren().stream().filter((o) -> StringUtils.equals(o.getName(), propName)).collect(Collectors.toList());
        if (list.size()==0) {
            return null;
        }
        if (list.size()>1) {
            throw new MessageVirtualException("resourceConfName={} 对应的属性名={}存在多个,resourceConfName={}").format(this.resourceConfName,propName);
        }
        return list.get(0);
    }


    public String getResourceConfName() {
        return resourceConfName;
    }

    public String getAuthor()
    {
        throw new MessageVirtualException("请使用 propValue(GenConfigProxy::getAuthor) 或者 propValue('author') 进行调用");
    }

    public String getPackageName()
    {
        throw new MessageVirtualException("请使用 propValue(GenConfigProxy::getXxxx) 或者 propValue('xxxx') 进行调用");
    }


    public boolean getAutoRemovePre()
    {
        throw new MessageVirtualException("请使用 propValue(GenConfigProxy::getXxxx) 或者 propValue('xxxx') 进行调用");
    }


    public String getTablePrefix()
    {
        throw new MessageVirtualException("请使用 propValue(GenConfigProxy::getXxxx) 或者 propValue('xxxx') 进行调用");
    }

    public boolean isColumnCamelCaseConvert() {
        throw new MessageVirtualException("请使用 propValue(GenConfigProxy::isXxxx) 或者 propValue('xxxx') 进行调用");
    }



    public static BaseGenConfig getBaseGenConfig(){
        return SpringUtils.getBean(BaseGenConfig.class);
    }

    public <T> String propValue(IGetter<T> fn){
        return propValue(convertToFieldName(fn));
    }

    public String propValue(String propName){
        return paramMap.get(propName);
    }

    public <T> boolean propValueIsTrue(IGetter<T> fn){
        return propValueIsTrue(convertToFieldName(fn));
    }

    public boolean propValueIsTrue(String propName){
        return BooleanUtils.isTrue(paramMap.get(propName));
    }
}

